package com.ruoyi.syf.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.ConvertField;
import com.ruoyi.common.annotation.ConvertFieldItem;
import com.ruoyi.common.enums.ConvertType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.syf.domain.RiskEvaluation;
import com.ruoyi.syf.domain.RiskEvaluationAccidentDetail;
import com.ruoyi.syf.domain.vo.RiskEvaluationVo;
import com.ruoyi.syf.mapper.RiskEvaluationMapper;
import com.ruoyi.syf.service.IRiskEvaluationAccidentDetailService;
import com.ruoyi.syf.service.IRiskEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 风险判定主Service业务层处理
 *
 * @author yr
 * @date 2024-07-04
 */
@Service
public class RiskEvaluationServiceImpl extends ServiceImpl<RiskEvaluationMapper, RiskEvaluation> implements IRiskEvaluationService {
    @Autowired
    private RiskEvaluationMapper riskEvaluationMapper;
    @Autowired
    private IRiskEvaluationAccidentDetailService evaluationAccidentDetailService;

    @Override
    public RiskEvaluationVo selectRiskEvaluationByUnitId(Long unitId) {
        return new RiskEvaluationVo();
    }

    /**
     * 查询风险判定主
     *
     * @param id 风险判定主主键
     * @return 风险判定主
     */
    @Override
    public RiskEvaluationVo selectRiskEvaluationById(Long id) {
        RiskEvaluation riskEvaluation = riskEvaluationMapper.selectById(id);
        RiskEvaluationVo vo = new RiskEvaluationVo();
        BeanUtils.copyBeanProp(vo,riskEvaluation);
        List<RiskEvaluationAccidentDetail> detailList =  evaluationAccidentDetailService.getListByEvaluationId(riskEvaluation.getId());
        List<Long> accidentList = detailList.stream().map(RiskEvaluationAccidentDetail::getAccidentTypeId).collect(Collectors.toList());
        vo.setAccidentList(accidentList);
        return vo;
    }

    /**
     * 查询风险判定列表
     *
     * @param riskEvaluation 风险判定
     * @return 风险判定
     */
    @ConvertField({
            @ConvertFieldItem(fieldName = "method", dictType = "decision_method", type = ConvertType.DICT),
            @ConvertFieldItem(fieldName = "maxAccidentLevel", dictType = "risk_level", type = ConvertType.DICT)
    })
    @Override
    public List<RiskEvaluationVo> list(RiskEvaluationVo riskEvaluation) {
        return riskEvaluationMapper.list(riskEvaluation);
    }

    /**
     * 新增风险判定主
     *
     * @param riskEvaluation 风险判定主
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertRiskEvaluation(RiskEvaluationVo riskEvaluation) {
        riskEvaluationMapper.insert(riskEvaluation);
        evaluationAccidentDetailService.saveList(riskEvaluation.getId(),riskEvaluation.getAccidentList());
        return true;
    }

    /**
     * 修改风险判定主
     *
     * @param riskEvaluation 风险判定主
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRiskEvaluation(RiskEvaluationVo riskEvaluation) {
        riskEvaluationMapper.updateById(riskEvaluation);
        evaluationAccidentDetailService.updateByEvaluationId(riskEvaluation.getId(),riskEvaluation.getAccidentList());
        return true;
    }

    /**
     * 批量删除风险判定主
     *
     * @param ids 需要删除的风险判定主主键
     * @return 结果
     */
    @Override
    public boolean deleteRiskEvaluationByIds(Long[] ids) {
        return riskEvaluationMapper.deleteRiskEvaluationByIds(ids) > 0;
//        evaluationAccidentDetailService.deleteByEvaluationId(Arrays.asList(ids));
//        return true;
    }

    /**
     * 删除风险判定主信息
     *
     * @param id 风险判定主主键
     * @return 结果
     */
    @Override
    public boolean deleteRiskEvaluationById(Long id) {
        return riskEvaluationMapper.deleteRiskEvaluationById(id) > 0;
    }
}
